<template>
    <el-card>
        <el-button type="primary" @click="dialogVisible = true">新建</el-button>

        <template #header>
            <div>
                菜单管理
            </div>
        </template>
        <el-table :data="menuData">
            <el-table-column label="ID" prop="id">
            </el-table-column>
            <el-table-column label="label" prop="label">
            </el-table-column>
            <el-table-column label="图标" prop="icon">

            </el-table-column>
            <el-table-column label="路径" prop="path">
            </el-table-column>
            <el-table-column label="上级菜单" prop="parent">
            </el-table-column>

            <el-table-column label="操作">
                <template #default="{ row }">
                    <el-button type="warning" @click="handleEdit(row)">编辑</el-button>
                    <el-button type="danger" @click="handleDelete(row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-row style="height:60px;" align="middle" justify="center">
            <el-pagination background layout="prev, pager, next" :total="total" v-model:page-size="pageSize"
                v-model:current-page="page" @current-change="getdata" />
        </el-row>
        <!-- 弹窗 -->
        <el-dialog v-model="dialogVisible" title="新增" width="500">
            <el-form label-width="80px" :model="addForm" :rules="rules" ref="formIns">

                <el-form-item label="名称" prop="label">
                    <el-input v-model="addForm.label"></el-input>
                </el-form-item>
                <el-form-item label="图标" prop="icon">
                    <el-select v-model="addForm.icon">
                        <el-option :value="item" v-for="item in iconData" :key="item" :label="item" >
                            <Component :is="item" style="width: 12px;"></Component>
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="路径" prop="path">
                    <el-input v-model="addForm.path"></el-input>
                </el-form-item>
                <el-form-item label="上级" prop="parent">
                    <el-input v-model="addForm.parent"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="handlesubmit">
                        确认
                    </el-button>
                </div>
            </template>
        </el-dialog>

        <!-- 编辑弹框 -->
        <el-dialog v-model="dialogVisible2" title="编辑" width="500">
            <el-form label-width="80px" :model="editForm" :rules="rules" ref="formIns2">
                <el-form-item label="名称" prop="label">
                    <el-input v-model="editForm.label"></el-input>
                </el-form-item>
                <el-form-item label="图标" prop="icon">
                    <el-select v-model="editForm.icon">
                        <el-option :value="item" v-for="item in iconData" :key="item" :label="item" >
                            <Component :is="item" style="width: 12px;"></Component>
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="路径" prop="path">
                    <el-input v-model="editForm.path"></el-input>
                </el-form-item>
                <el-form-item label="上级菜单" prop="parent">
                    <el-input v-model="editForm.parent"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogVisible2 = false">取消</el-button>
                    <el-button type="primary" @click="handleSubmit2">
                        确认
                    </el-button>
                </div>
            </template>
        </el-dialog>
    </el-card>
</template>
<script setup lang="ts">
// 弹框

const open = () => {
    ElMessageBox.alert('This is a message', 'Title', {
        // if you want to disable its autofocus
        // autofocus: false,
        confirmButtonText: 'OK',
        callback: (action: Action) => {
            ElMessage({
                type: 'info',
                message: `action: ${action}`,
            })
        },
    })
}
import { ref, reactive, onMounted } from 'vue';
import { getMenu, addMenu, delMenu, edtMenu } from '@/api/api';
import type { FormRules, FormInstance } from "element-plus"
import { ElMessage, ElMessageBox } from 'element-plus'
import type { Action } from 'element-plus'
import { ElementPlus } from '@element-plus/icons-vue';
import * as ElementPlusIconsVue from  '@element-plus/icons-vue';
const iconData=Object.keys(ElementPlusIconsVue)

// 查看
const menuData = ref<MenuData[]>([]);
onMounted(() => {
    getdata();
})
const pageSize = ref(5);
const total = ref(0);
const page = ref(1);
const getdata = async () => {
    const res = await getMenu({
        page: page.value,
        limit: pageSize.value
    });
    menuData.value = res.data.data;
    total.value = res.data.total
}
// 新增数据
const addForm = reactive<MenuData>({
    label: "",
    icon: "",
    path: "",
    parent: "",

});
const rules: FormRules = {
    label: { required: true, message: "请输入label", trigger: "blur" },
    icon: { required: true, message: "请输入icon", trigger: "blur" },
    path: { required: true, message: "请输入path", trigger: "blur" }
}
const dialogVisible = ref(false)
const formIns = ref<FormInstance>();
const handlesubmit = async () => {
    formIns.value?.validate(async (valid) => {
        if (valid) {
            const res = await addMenu(addForm)
            if (res.data.code === 200) {
                ElMessage.success("修改成功");
                dialogVisible.value = false;
                formIns.value?.resetFields();
                getdata();
            } else {
                ElMessage.error("修改失败")
            }
        }
    })
}
// 删除
const handleDelete = async (id: number) => {
    try {
        await ElMessageBox.confirm(
            '确认删除当前角色吗',
            '警告',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
        const res = await delMenu(id);
        if (res.data.code === 200) {
            ElMessage.success("删除成功");
            getdata();
        } else {
            ElMessage.error("删除失败");
        }
    } catch (e) {
        ElMessage.info("取消删除")
    }
}
//编辑
// const handleChange = async (val: number, id: number) => {
//     const res = await edtMenu(id, { state: val });
//     if (res.data.code === 200) {
//         ElMessage.success("修改成功");
//     }
// }

const dialogVisible2 = ref(false);
const editForm = ref<MenuData>({

    label: "",
    icon: "",
    path: "",
    parent: "",
});
const formIns2 = ref<FormInstance>();
let eidtid = 0;
const handleEdit = (body: MenuData) => {
    dialogVisible2.value = true;
    editForm.value = body;
    eidtid = body.id as number;
}
const handleSubmit2 = () => {
    formIns2.value?.validate(async valid => {
        if (valid) {
            const res = await edtMenu(eidtid, editForm.value)
            if (res.data.code === 200) {
                dialogVisible2.value = false;
                getdata();
                ElMessage.success("修改成功");
            } else {
                ElMessage.error("删除失败");
            }
        }
    })
}
</script>
<style scoped></style>